import 'package:cashbook/utils/db_util/dbmanager.dart';
import 'package:sqflite/sqflite.dart';

class User {
  int autoId;
  String userUuid;
  String name;
  String psd;
  String nickName;
  String ask1;
  String ask2;
  String answer1;
  String answer2;

  Map<String, dynamic> toMap() {
    var map = <String, dynamic>{
      columnUserUuid: userUuid,
      columnName: name,
      columnPsd: psd,
      columnNickName: nickName,
      columnAsk1: ask1,
      columnAsk2: ask2,
      columnAnswer1: answer1,
      columnAnswer2: answer2,
    };
    if (autoId != null) {
      map[columnAutoId] = autoId;
    }
    return map;
  }

  User();

  User.fromMap(Map<String, dynamic> map) {
    autoId = map[columnAutoId];
    name = map[columnName];
    userUuid = map[columnUserUuid];
    psd = map[columnPsd];
    nickName = map[columnNickName];
    ask1 = map[columnAsk1];
    ask2 = map[columnAsk2];
    answer1 = map[columnAnswer1];
    answer2 = map[columnAnswer2];
  }

  @override
  String toString() {
    return 'User{autoId: $autoId, userUuid: $userUuid,name: $name, psd: $psd, nickName: $nickName, ask1: $ask1, ask2: $ask2, answer1: $answer1, answer2: $answer2}';
  }
}

final String tableUser = 'user';
final String columnAutoId = 'autoId';
final String columnName = 'name';
final String columnUserUuid = 'userUuid';
final String columnPsd = 'psd';
final String columnNickName = 'nickName';
final String columnAsk1 = 'ask1';
final String columnAsk2 = 'ask2';
final String columnAnswer1 = 'answer1';
final String columnAnswer2 = 'answer2';

class UserProvider {
  Database db;

  String _createTableSql = '''
  create table ${tableUser} (
    ${columnAutoId} integer primary key autoincrement,
    ${columnUserUuid} text not null,
    ${columnName} text not null,
    ${columnPsd} text not null,
    ${columnNickName} text not null,
    ${columnAsk1} text ,
    ${columnAsk2} text ,
    ${columnAnswer1} text ,
    ${columnAnswer2} text 
     )
  ''';

  Future createTable() async {
    DBManager.createTable(tableUser, _createTableSql);
  }

  insert(User user) async {
    User fuser =await findByName(user);
    if (null == fuser) {
      user.autoId = await DBManager.getDb().insert(tableUser, user.toMap());
    } else {
      user.autoId = fuser.autoId;
      user.autoId = await DBManager.getDb().update(tableUser, user.toMap());
    }

    return user;
  }

  findAll() async {
    List<Map> maps = await DBManager.getDb().query(tableUser);

    List<User> userList = List();
    for (int i = 0; i < maps.length; i++) {
      User u = User.fromMap(maps[i]);
      if (u != null) userList.add(u);
      print(u.toString());
    }
    return userList;
  }

  getById(int id) async {
    List<Map> maps = await DBManager.getDb().query(tableUser,
        columns: [
          columnAutoId,
          columnUserUuid,
          columnName,
          columnPsd,
          columnNickName,
          columnAsk1,
          columnAsk2,
          columnAnswer1,
          columnAnswer2
        ],
        where: '${columnAutoId} = ?',
        whereArgs: [id]);
    if (maps.length > 0) {
      return User.fromMap(maps.first);
    }
    return null;
  }

  delete(int id) async {
    return await DBManager.getDb()
        .delete(tableUser, where: '${columnAutoId} = ?', whereArgs: [id]);
  }

  findByName(User user) async {
    List<Map> maps = await DBManager.getDb().query(tableUser,
        columns: [
          columnAutoId,
          columnUserUuid,
          columnName,
          columnPsd,
          columnNickName,
          columnAsk1,
          columnAsk2,
          columnAnswer1,
          columnAnswer2
        ],
        where: '${columnName} = ?',
        whereArgs: [user.name]);
    if (maps.length > 0) {
      return User.fromMap(maps.first);
    }
    return null;
  }

  update(User user) async {

    return await DBManager.getDb().update(tableUser,user.toMap(),where: '${columnName} = ?',whereArgs: [user.name]);
  }
}
